package com.yqg.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.*;
import com.yqg.education.mapper.*;
import com.yqg.education.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * StageServiceImpl 服务实现类
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@Service
public class StageServiceImpl extends ServiceImpl<StageMapper, Stage> implements IStageService {
    @Resource
    private ITrainClassService trainClassService;
    @Resource
    private ClassStudentMapper classStudentMapper;

    @Resource
    private TrainSignMapper trainSignMapper;

    @Resource
    private ExamSignMapper examSignMapper;

    @Resource
    private ExamLogMapper examLogMapper;

    @Resource
    private PeriodMapper periodMapper;

    @Resource
    private TeachPlanMapper teachPlanMapper;

    @Resource
    private IClassCourseService classCourseService;

    @Resource
    private ICourseService courseService;

    @Resource
    private ITeachPlanService teachPlanService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IUserService userService;
    @Resource
    private IStuCertificateService stuCertificateService;

    @Resource
    private StuFeedbackMapper stuFeedbackMapper;
    @Resource
    private TeachEvaluateMapper teachEvaluateMapper;

    @Override
    public Result getTraining(Long id) {
        Map<String, Object> map = new HashMap<>();

//         获取参与培训的班级信息
        LambdaQueryWrapper<TrainClass> trainClassWrapper = new LambdaQueryWrapper<>();
        trainClassWrapper.eq(TrainClass::getStageId, id);
        trainClassWrapper.eq(TrainClass::getIsDelete, 0);
        List<TrainClass> trainClassList = trainClassService.list(trainClassWrapper);

        if (trainClassList == null || trainClassList.size() == 0) {
            return Result.error("500", "当前档期下没有班级");
        }

//        这一期的所有学员ID
        List<Long> stuIdList = new ArrayList<>();
//        这一期的所有课程签到率
        Map<String, Object> classSignReta = new HashMap<>();
//        这一期的所有课程考试签到率
        Map<String, Object> examSignReta = new HashMap<>();
//        这一期所有班主任和教师id
        List<Long> manageIds = new ArrayList<>();
        List<Long> teacherIds = new ArrayList<>();
//        这一期的所有课时、教案、教师团队、班主任团队
        List<Period> periodList = new ArrayList<>();
        List<TeachPlan> teachPlanList = new ArrayList<>();
        List<String> teacherList = new ArrayList<>();
        List<String> manageList = new ArrayList<>();

//        这一期课堂日志信息等信息
        List<String> classroomJournalList = new ArrayList<>();
        List<String> classSummaryList = new ArrayList<>();
        List<String> trainingList = new ArrayList<>();
//        课程反馈与教师评价
        List<Map<String,Object>> courseFeedbacksList = new ArrayList<>();
        List<Map<String,Object>> feedbacksList = new ArrayList<>();

//        对每个班级进行的操作
        trainClassList.forEach(trainClass -> {
            // 查询每一个班级的学员
            List<Long> stuIds = classStudentMapper.getStuIdByClassId(trainClass.getId());
            stuIdList.addAll(stuIds);
            // 查询每一个班级的每门课程的签到率
            List<Map<String, Object>> signRetaList = trainSignMapper.getSignRate(trainClass.getId());
            classSignReta.put(trainClass.getTrainClassNum(), signRetaList);
            // 查询每一个班级的每门课程的考试签到率
            List<Map<String, Object>> examSignRetaList = examSignMapper.getExamSignRate(trainClass.getId());
            examSignReta.put(trainClass.getTrainClassNum(), examSignRetaList);

            // 查询课程Id
            List<Long> courseIds = new ArrayList<>();
            LambdaQueryWrapper<ClassCourse> classCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classCourseLambdaQueryWrapper.eq(ClassCourse::getClassId, trainClass.getId());
            classCourseService.list(classCourseLambdaQueryWrapper).forEach(course -> {
                courseIds.add(course.getCourseId());
            });

            if (courseIds.size() > 0) {
                // 查询课时
                periodList.addAll(periodMapper.getPdfData(courseIds));
                // 查询教案
                teachPlanList.addAll(teachPlanMapper.getPdfData(courseIds));

                // 获取老师和班主任id
                LambdaQueryWrapper<Period> periodLambdaQueryWrapper = new LambdaQueryWrapper<>();
                periodLambdaQueryWrapper.in(Period::getCourseId, courseIds);
                periodLambdaQueryWrapper.eq(Period::getIsDelete, 0);
                periodService.list(periodLambdaQueryWrapper).forEach(period -> {
                    teacherIds.add(period.getTeachId());
                });
            }

            manageIds.add(trainClass.getManageId());

            // 获取课堂日志
            LambdaQueryWrapper<StuCertificate> stuCertificateLambdaQueryWrapper = new LambdaQueryWrapper<>();
            stuCertificateLambdaQueryWrapper.eq(StuCertificate::getClassId, trainClass.getId());
            stuCertificateLambdaQueryWrapper.eq(StuCertificate::getIsDelete, 0);
            stuCertificateService.list(stuCertificateLambdaQueryWrapper).forEach(stuCertificate -> {
                classroomJournalList.add(stuCertificate.getPeriodLog());
                classSummaryList.add(stuCertificate.getClassSum());
                trainingList.add(stuCertificate.getTrainSitu());
            });

            // 获取课程反馈与教师反馈
            stuFeedbackMapper.getFeedbackByClassId(trainClass.getId()).forEach(stuFeedback -> {
                Map<String, Object> feedbackMap = new HashMap<>();
                feedbackMap.put("name",stuFeedback.getStuname());
                feedbackMap.put("department",trainClass.getTrainClassNum());
                feedbackMap.put("courseName",stuFeedback.getCourseName());
                feedbackMap.put("comment",stuFeedback.getPeriodFeedback());
                courseFeedbacksList.add(feedbackMap);
            });

            teachEvaluateMapper.getEvaluateByClassId(trainClass.getId()).forEach(teachEvaluate -> {
                Map<String, Object> evaluateMap = new HashMap<>();
                evaluateMap.put("name",teachEvaluate.getStuname());
                evaluateMap.put("department",trainClass.getTrainClassNum());
                evaluateMap.put("teachName",teachEvaluate.getTeachName());
                evaluateMap.put("comment",teachEvaluate.getEvaluate());
                feedbacksList.add(evaluateMap);
            });


        });
        // 查询教师团队
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (teacherIds.size() > 0) {
            userLambdaQueryWrapper.in(User::getId, teacherIds);
            userLambdaQueryWrapper.eq(User::getIsDelete, 0);
            userService.list(userLambdaQueryWrapper).forEach(user -> {
                teacherList.add(user.getUsername());
            });
        }

        // 查询班主任团队
        userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (manageIds.size() > 0) {
            userLambdaQueryWrapper.in(User::getId, manageIds);
            userLambdaQueryWrapper.eq(User::getIsDelete, 0);
            userService.list(userLambdaQueryWrapper).forEach(user -> {
                manageList.add(user.getUsername());
            });
        }

//        获取这期学员考试信息
        List<ExamLog> examInfoList = new ArrayList<>();
        if (stuIdList.size() > 0) {
            examInfoList = examLogMapper.getInfoByStuId(stuIdList);
        }

        map.put("participants", stuIdList.size());
        map.put("classSignReta", classSignReta);
        map.put("examSignReta", examSignReta);
        map.put("topStudents", examInfoList);
        map.put("planItems", periodList);
        map.put("materials", teachPlanList.stream().distinct().collect(Collectors.toList()));
        map.put("teachers", teacherList.stream().distinct().collect(Collectors.toList()));
        map.put("manages", manageList.stream().distinct().collect(Collectors.toList()));
        map.put("periodLog", classroomJournalList.stream().distinct().collect(Collectors.toList()));
        map.put("classSum", classSummaryList.stream().distinct().collect(Collectors.toList()));
        map.put("trainSitu", trainingList.stream().distinct().collect(Collectors.toList()));
        map.put("courseFeedbacks",courseFeedbacksList);
        map.put("feedbacks",feedbacksList);

        return Result.success(map);
    }
}
